package com.czn.bookshop.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czn.bookshop.entity.Book;
import com.czn.bookshop.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.security.PublicKey;

/**
 * 商品首页控制器
 *
 * @author ChenZeNan
 * @create 2020-12-25 22:39
 */

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookService;


    /**
     * 跳转到书城首页
     *
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    /**
     * 根据标签和页数获取图书信息
     *
     * @param model
     * @param page
     * @param category
     * @return
     */
    @RequestMapping("/getBookData")
    public String getBookData(Model model, Integer page, Integer category) {
        // MyBatis Plus 分页
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", category);
        IPage<Book> iPage = bookService.page(new Page<>(page, 4), queryWrapper); //以category字段作为分页的依据，每四条记录为一页
        model.addAttribute("bookList", iPage.getRecords());//获取所有的记录
        model.addAttribute("pre", iPage.getCurrent() - 1);//上一页数
        model.addAttribute("next", iPage.getCurrent() + 1);//下一页数
        model.addAttribute("category", category);//当前标签回显
        model.addAttribute("cur", iPage.getCurrent());// 当前的页数
        model.addAttribute("last", iPage.getPages()); //总页数
        return "bookData";
    }

    /**
     * 分类图书页面跳转
     *
     * @param category
     * @param model
     * @return
     */
    @RequestMapping("/bookList")
    public String bookList(Integer category, Model model) {
        model.addAttribute("category", category); // 把category值放到model中，方便books_list页面的使用
        return "books_list";
    }

    /**
     * 根据标签category查询所有具有category的book数据
     *
     * @param category
     * @return
     */
    @RequestMapping("/getBookListData")
    public String getBookListData(Integer page, Integer category, Integer pageSize, Model model) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", category);
        IPage<Book> iPage = bookService.page(new Page<>(page, pageSize), queryWrapper);
        model.addAttribute("bookList", iPage.getRecords());
        model.addAttribute("pre", iPage.getCurrent() - 1);//上一页数
        model.addAttribute("next", iPage.getCurrent() + 1);//下一页数
        model.addAttribute("category", category);//当前标签回显
        model.addAttribute("cur", iPage.getCurrent());// 当前的页数
        model.addAttribute("pages", iPage.getPages()); //总页数
        model.addAttribute("pageSize", pageSize); //一页显示的图书数量
        return "bookListData";
    }

    @RequestMapping("/detail")
    public String getBookDetail(Integer bookId, Model model) {
        Book book = bookService.getById(bookId);
        model.addAttribute("book", book);
        return "details";
    }
}
